package ningbo.ticket;

/**
 * @Author 宁铂
 * @Date 2021/7/28 19:58
 * @Version 1.0
 */
public class TestTicket {
    /** 通过继承的方法实现多线程售票案例
     *  要求: 4个窗口 总共100张票,售完为止
     * */
    public static void main(String[] args) {
        TicketT ticketT1 = new TicketT("窗口一");
        TicketT ticketT2 = new TicketT("窗口二");
        TicketT ticketT3 = new TicketT("窗口三");
        TicketT ticketT4 = new TicketT("窗口四");

        ticketT1.start();
        ticketT2.start();
        ticketT3.start();
        ticketT4.start();

    }
}


class TicketT extends Thread{

    public TicketT(String name) {
        super(name);
    }

    static int tickets = 100;
    //static Object o = new Object();

    @Override
    public void run() {

        while (true){
            synchronized (TicketT.class) {
                if(tickets >0) {
                    System.out.println(getName() + "=" + tickets--);
                    if (tickets <= 0) break;
                }
            }
        }
    }
}